<!DOCTYPE html>
<HTML>
<HEAD>
    <TITLE> ZTREE DEMO </TITLE>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="plugins/ztree/css/zTreeStyle/zTreeStyle.css">
    <script src="plugins/ztree/js/jquery-1.4.4.min.js"></script>
    <script src="plugins/ztree/js/jquery.ztree.all-3.5.js"></script>
    <SCRIPT LANGUAGE="JavaScript">
        var zTreeObj;
        // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
        var setting = {
            data: {
                simpleData: {
                    enable: true,  //要使用简单json数据格式
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            },
            check: {
                enable: true  //在树形节点上显示复选框
            }

        };
        // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
        /*var zNodes = [    //标准数据结构
            {name:"test1", open:true, children:[
                    {name:"test1_1"},
                    {name:"test1_2"}]},
            {name:"test2", open:true, children:[
                    {name:"test2_1"},
                    {name:"test2_2"}]},
            {name:"test3", open:true, children:[
                    {name:"test3_1"},
                    {name:"test3_2",open:true,children:[{name:"3_2_1"}]}]}
        ];*/
        var zNodes =[
            {id:1, pId:0, name: "父节点1"},
            {id:11, pId:1, name: "子节点1_1"},
            {id:12, pId:1,checked:true, name: "子节点1_2"},
            {id:2, pId:0, name: "父节点2"},
            {id:21, pId:2, name: "子节点2_1"},
            {id:22, pId:2, name: "子节点2_2"},
            {id:221, pId:22, name: "子节点2_2_1"},
            {id:222, pId:22, name: "子节点2_2_2"}
        ]
        $(document).ready(function(){
            zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            zTreeObj.expandAll(true);//展开全部节点
        });

        function save() {
            var nodes = zTreeObj.getCheckedNodes(true);  //表示获取 被勾选 的节点集合
            for (let i = 0; i < nodes.length; i++) {
                alert( nodes[i].id)
            }
        }
    </SCRIPT>
</HEAD>
<BODY>
<div>
    <button onclick="save()">保存</button>
    <ul id="treeDemo" class="ztree"></ul>
</div>
</BODY>
</HTML>